using ComponentFactory.Krypton.Toolkit;

using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Integration.WinForms;

using System;
using System.ComponentModel;
using System.Windows.Forms;


namespace Agron.UI.SmartClient
{
    /// <summary>
    /// Defines the base <see cref="KryptonForm"/> for all the application's dialogs.
    /// </summary>
    partial class DialogBase : KryptonForm
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="DialogBase"/> class.
        /// </summary>
        public DialogBase()
        {
            this.InitializeComponent();
            
            this.loadBackgroundWorker.RunWorkerAsync();
            this.validationProvider.ValidationPerformed += new EventHandler<ValidationPerformedEventArgs>(validationProvider_ValidationPerformed);
        }

        /// <summary>
        /// Gets or sets the data source that the connector binds to.
        /// </summary>
        /// <value>An <see cref="object"/> that acts as a data source. The default is null.</value>
        public object DataSource
        {
            get { return this.bindingSource.DataSource; }
            set
            {
                this.bindingSource.DataSource = value;
                this.OnDataSourceChanged(EventArgs.Empty);
            }
        }

        /// <summary>
        /// Occurs when the valuefor the <see cref="DialogBase.DataSource"/> has changed.
        /// </summary>
        public event EventHandler DataSourceChanged;

        /// <summary>
        /// Raises the <see cref="E:DataSourceChanged"/> event.
        /// </summary>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected virtual void OnDataSourceChanged(EventArgs e)
        {
            EventHandler eventHandler = this.DataSourceChanged;
            if (null != eventHandler)
                eventHandler(this, e);
        }

        /// <summary>
        /// Gets a reference to the <see cref="BackgroundWorker"/> used for data loading purposes.
        /// </summary>
        /// <value>
        /// A reference to the <see cref="BackgroundWorker"/> used for data loading purposes.
        /// </value>
        protected BackgroundWorker LoadBackgroundWorker { get { return this.loadBackgroundWorker; } }

        /// <summary>
        /// Gets a reference to the <see cref="BackgroundWorker"/> used for data saving purposes.
        /// </summary>
        /// <value>
        /// A reference to the <see cref="BackgroundWorker"/> used for data saving purposes.
        /// </value>
        protected BackgroundWorker AcceptActionBackgroundWorker { get { return this.acceptActionBackgroundWorker; } }

        /// <summary>
        /// Handles the Load event of the DialogBase <see cref="Form"/>.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void DialogBase_Load(object sender, EventArgs e)
        {
            if (this.loadBackgroundWorker.IsBusy)
                base.Cursor = Cursors.WaitCursor;
        }

        /// <summary>
        /// Handles the ValidationPerformed event of the validationProvider control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Microsoft.Practices.EnterpriseLibrary.Validation.Integration.WinForms.ValidationPerformedEventArgs"/> instance containing the event data.</param>
        private void validationProvider_ValidationPerformed(object sender, ValidationPerformedEventArgs e)
        {
            this.okButton.Enabled = e.ValidationResults.IsValid;
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            base.ValidateChildren();

            base.Cursor = Cursors.WaitCursor;
            this.acceptActionBackgroundWorker.RunWorkerAsync(this.bindingSource.DataSource);
        }

        /// <summary>
        /// Gets or sets the button on the form that is clicked when the user presses the ENTER key.
        /// </summary>
        /// <value></value>
        /// <returns>An <see cref="KryptonButton" /> that represents the button to use as the accept button for the form.</returns>
        protected new KryptonButton AcceptButton { get { return this.okButton; } }

        /// <summary>
        /// Gets or sets the button control that is clicked when the user presses the ESC key.
        /// </summary>
        /// <value></value>
        /// <returns>An <see cref="KryptonButton" /> that represents the button to use as the cancel button for the form.</returns>
        protected new KryptonButton CancelButton { get { return this.cancelButton; } }

        /// <summary>
        /// Handles the DoWork event of the loadBackgroundWorker control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.ComponentModel.DoWorkEventArgs"/> instance containing the event data.</param>
        protected virtual void OnLoadBackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
        {
        }

        /// <summary>
        /// Handles the RunWorkerCompleted event of the loadBackgroundWorker control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.ComponentModel.RunWorkerCompletedEventArgs"/> instance containing the event data.</param>
        protected virtual void OnLoadBackgroundWorkerRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            base.Cursor = Cursors.Default;
        }

        /// <summary>
        /// Handles the DoWork event of the acceptActionBackgroundWorker control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.ComponentModel.DoWorkEventArgs"/> instance containing the event data.</param>
        protected virtual void OnAcceptActionBackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
        {
        }

        /// <summary>
        /// Handles the RunWorkerCompleted event of the acceptActionBackgroundWorker control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.ComponentModel.RunWorkerCompletedEventArgs"/> instance containing the event data.</param>
        protected virtual void OnAcceptActionBackgroundWorkerRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            base.Cursor = Cursors.Default;

            if (null == e.Error)
            {
                base.DialogResult = DialogResult.OK;
                this.Close();
            }
            else if (ExceptionPolicy.HandleException(e.Error, Constants.SmartClientExceptionPolicy))
            {
                MessageBoxService.ReportError(this, e.Error.Message);
            }
        }
    }
}